var nowServletName = null;

function intoServletList() {
	if (nowProjectIndex >= 0) {
		getServletList(nowProjectIndex);
		listDivKind = "servlet";
		openListDiv();
	}
}


function getServletList() {
	send("GET", "/@ServerApplet?" + nowProjectIndex, true, "", showServletList, null);
}

function removeServlet(fileName) {
	if (confirm("是否删除应用\n" + "\"" + fileName + "\"")) {
		rat.qure();
		var json = {};
		json.projectIndex = nowProjectIndex;
		json.order = "Remove";
		json.fileName = fileName;
		send("POST", "/@ServerApplet", true, JSON.stringify(json), showServletList, null);
	}
}

function getServlet(fileName) {
	nowServletName = fileName;
	var json = {};
	json.projectIndex = nowProjectIndex;
	json.order = "Get";
	json.fileName = fileName;
	send("POST", "/@ServerApplet", true, JSON.stringify(json), showServletCode, null);
	document.getElementById("CodeChange").onclick = changeServletCode;
	openCodeDiv();
}

function changeServletCode() {
	if (nowServletName != null) {
		var code = editor.getValue();
		rat.qure();
		var json = {};
		json.projectIndex = nowProjectIndex;
		json.order = "Change";
		json.fileName = nowServletName;
		json.context = code;
		send("POST", "/@ServerApplet", true, JSON.stringify(json), showServletCode, null);
	}
}

function setServletUrl(fileName) {
	var str = prompt("请输入新定位", "/@NewUrl");
	if (str != null) {
		rat.qure();
		var json = {};
		json.projectIndex = nowProjectIndex;
		json.order = "SetUrl";
		json.fileName = fileName;
		json.context = str;
		send("POST", "/@ServerApplet", true, JSON.stringify(json), showServletList, null);
	}
}

function addServlet() {
	var str = prompt("请输入新应用名", "NewServlet");
	if (str != null) {
		rat.qure();
		var json = {};
		json.projectIndex = nowProjectIndex;
		json.order = "Add";
		json.fileName = str;
		send("POST", "/@ServerApplet", true, JSON.stringify(json), showServletList, null);
	}
}

function showServletList(text) {
	var servletList = JSON.parse(text);
	var html = "";
	for (let i = 0; i < servletList.length; i++) {
		html += "<lable>定位</lable>";
		html += "<input type=\"button\" name=\"url\" class=\"name\" onclick=\"setServletUrl('" + servletList[i]
			.servletName + "')\" value=\"";
		html += servletList[i].urlMapping;
		html += "\"/>";
		html += "<lable>应用</lable>";
		html += "<input type=\"button\" name=\"name\" class=\"name\" onclick=\"getServlet('" + servletList[i]
			.servletName + "')\" value=\"";
		html += servletList[i].servletName;
		html += "\"/>";
		html += "<button type=\"button\" class=\"remove\" onclick=\"removeServlet('" + servletList[i].servletName +
			"')\">删</button>";
		html += "<br>";
	}
	html += "<br><button type=\"button\" class=\"add\" onclick=\"addServlet()\">添加</button>";
	document.getElementById("ListTitle").innerHTML = "应用列表";
	document.getElementById("ListContent").innerHTML = html;
}

function showServletCode(text) {
	document.getElementById("CodeTitle").innerHTML = "" + nowServletName;
	var code = JSON.parse(text);
	editor.setValue(code);
}
